/************************************************************************
                                                                     
   Xceed Ultimate ListBox for Silverlight                                                                                                                                            
   Copyright (C) 2010 Xceed Software Inc.    
                                                                     
   This program is provided to you under the terms of the GNU General Public  
   License version 2 as published by the Free Software Foundation. 
        
   This program is distributed in the hope that it will be useful, but
   WITHOUT ANY WARRANTY, without even the implied warranty of 
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
   General Public License for more details.

   You should have received a copy of the GNU General Public License along 
   with this program, if not, write to the Free Software Foundation, Inc., 
   51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.

   This program can be provided to you by Xceed Software Inc. under a
   proprietary commercial license agreement for use in non-Open Source
   projects. Visit Xceed.com to find the commercial edition of 
   Xceed Ultimate ListBox for Silverlight.                                    
                                                                      
 **********************************************************************/

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Data;
using System.Diagnostics;
using System.Globalization;
using System.Collections;

namespace Xceed.Silverlight.ListBox
{
  internal class BindingPathValueExtractor : FrameworkElement
  {
    #region CONSTRUCTORS

    public BindingPathValueExtractor( PropertyPath propertyPath )
    {
      m_binding = new Binding();

      m_binding.Path = propertyPath;
      m_binding.Mode = BindingMode.OneWay;

      this.SetBinding( BindingPathValueExtractor.ValueProperty, m_binding );
    }

    #endregion CONSTRUCTORS

    #region ValueProperty

    private static object UninitializedValueKey = new object();

    private static readonly DependencyProperty ValueProperty = DependencyProperty.Register(
      "Value", typeof( object ), typeof( BindingPathValueExtractor ), new PropertyMetadata( BindingPathValueExtractor.UninitializedValueKey ) );

    private object Value
    {
      get
      {
        object value = this.GetValue( BindingPathValueExtractor.ValueProperty );

        if( value == BindingPathValueExtractor.UninitializedValueKey )
          value = null;

        if( value == DependencyProperty.UnsetValue ) 
          return null;

        return value;
      }
    }

    #endregion ValueProperty

    #region PUBLIC METHODS

    public object GetValueFromItem( object item )
    {
      try
      {
        this.DataContext = item;

        return this.Value;
      }
      finally
      {
        // Might want to not set to null so that the binding is not re-invalidated.
        this.DataContext = null;
      }
    }

    #endregion PUBLIC METHODS

    #region PRIVATE FIELDS

    private Binding m_binding;

    #endregion PRIVATE FIELDS
  }
}
